import { Inject, Provide } from '@midwayjs/core';
import { InjectEntityModel } from '@midwayjs/typeorm';
import { Seller } from '../entity/seller.entity';
import { Like, Repository } from 'typeorm';
import Utils from '../utils';
import { SellerAddDto, SellerPageQueryDto } from '../dto/seller.dto';

@Provide()
export class SellerService {
  @InjectEntityModel(Seller)
  private sellerModel: Repository<Seller>;

  @Inject()
  private utils: Utils;

  /**
   * 注册经销商
   * @param name 经销商名称
   * @param phone 手机号
   * @param province 省份
   * @returns
   */
  public async addSeller(dto: SellerAddDto) {
    const existSeller = await this.sellerModel.findOne({
      where: {
        phone: dto.phone
      }
    });
    if (existSeller) {
      return this.utils.response.fail('该手机号已注册为经销商');
    }

    const seller = new Seller();
    seller.name = dto.name;
    seller.phone = dto.phone;
    seller.province = dto.province;
    seller.status = Seller.STATUS_ENABLE;
    await this.sellerModel.save(seller);
    return this.utils.response.success(seller.id, '注册成功');
  }

  /**
   * 删除经销商
   * @param sellerId 经销商ID
   * @returns
   */
  public async deleteSeller(sellerId: number) {
    const seller = await this.sellerModel.findOne({
      where: {
        id: sellerId
      }
    });
    if (!seller) {
      return this.utils.response.fail('经销商不存在');
    }
    await this.sellerModel.remove(seller);
    return this.utils.response.success(sellerId, '删除成功');
  }

  /**
   * 启用经销商
   * @param sellerId 经销商ID
   */
  public async enableSeller(sellerId: number) {
    const seller = await this.sellerModel.findOne({
      where: {
        id: sellerId
      }
    });
    if (!seller) {
      return this.utils.response.fail('经销商不存在');
    }
    seller.status = Seller.STATUS_ENABLE;
    await this.sellerModel.save(seller);
    return this.utils.response.success(sellerId, '启用成功');
  }

  /**
   * 禁用经销商
   * @param sellerId 经销商ID
   */
  public async disableSeller(sellerId: number) {
    const seller = await this.sellerModel.findOne({
      where: {
        id: sellerId
      }
    });
    if (!seller) {
      return this.utils.response.fail('经销商不存在');
    }
    seller.status = Seller.STATUS_DISABLE;
    await this.sellerModel.save(seller);
    return this.utils.response.success(sellerId, '禁用成功');
  }

  /**
   * 查询经销商列表
   * @param query
   */
  public async listSeller(
    query: Pick<SellerPageQueryDto, 'name' | 'phone' | 'status' | 'province'>
  ) {
    const { name, phone, status, province } = query;

    const where: any = {};
    if (name) {
      where.name = Like(`%${name}%`);
    }
    if (phone) {
      where.phone = Like(`%${phone}%`);
    }
    if (status) {
      where.status = status;
    }
    if (province) {
      where.province = Like(`%${province}%`);
    }

    const list = await this.sellerModel.find({
      where,
      order: {
        id: 'DESC'
      }
    });
    return this.utils.response.success(list);
  }

  public async pageListSeller(query: SellerPageQueryDto) {
    const { current, pageSize, name, phone, status, province } = query;
    const where: any = {};
    if (name) {
      where.name = Like(`%${name}%`);
    }
    if (phone) {
      where.phone = Like(`%${phone}%`);
    }
    if (status) {
      where.status = status;
    }
    if (province) {
      where.province = Like(`%${province}%`);
    }
    const [list, total] = await this.sellerModel.findAndCount({
      where,
      skip: (current - 1) * pageSize,
      take: pageSize,
      order: {
        id: 'DESC'
      }
    });
    return this.utils.response.success({
      list,
      total,
      current,
      pageSize
    });
  }
}
